package com.example.docking.service.impl;


import com.example.docking.dao.jindie.GenericDataItem;
import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.response.UserData;
import com.example.docking.service.UserService;
import com.example.docking.util.Constants;
import com.example.docking.util.Decrypt;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {

    @Override
    public ApiResult selectUserList() throws Exception {
        return sendApiRequestGet(Constants.USERPATH, map, "");
    }


    public static void main(String[] args) throws Exception {
        UserServiceImpl list = new UserServiceImpl();
        ApiResult result = list.selectUserList();
        System.out.println(result.getBody());
        Gson gson = new Gson();
        Type type1 = new TypeToken<ResponseData<GenericDataItem<UserData>>>() {
        }.getType();
        ResponseData<GenericDataItem<UserData>> response = gson.fromJson(result.getBody(), type1);
        System.out.println("Code: " + response.getErrcode());

        Decrypt decrypt = new Decrypt();
        GenericDataItem<UserData> genericDataItem = response.getData();
        List<UserData> userData = genericDataItem.getRows();

        for (UserData row : userData) {
            String encryptedPhone = row.getPhone();
            String decryptedPhone = decrypt.decrypt(encryptedPhone);
            row.setPhone(decryptedPhone);
        }
        System.out.println(response.toString());
    }

}